package com.boot.app.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.app.base.domain.BaseRole;
import com.boot.app.base.domain.BaseRoleAppRelation;
import com.boot.app.base.mapper.BaseRoleMapper;
import com.boot.app.base.service.IBaseRoleAppRelationService;
import com.boot.app.base.service.IBaseRoleService;
import com.boot.app.base.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BaseRoleServiceImpl extends ServiceImpl<BaseRoleMapper, BaseRole> implements IBaseRoleService {

    private final BaseRoleMapper mapper;

    private final IBaseRoleAppRelationService roleAppRelationService;

    @Autowired
    public BaseRoleServiceImpl(
        BaseRoleMapper mapper,
        IBaseRoleAppRelationService roleAppRelationService
    ){
        this.mapper = mapper;
        this.roleAppRelationService = roleAppRelationService;
    }

    @Override
    public List<BaseRole> fetchListByUserId(long userId) {
        return mapper.selectListByUserId(userId);
    }


    @Override
    @Transactional
    public boolean removeById(long id){
        mapper.deleteById(id);

        LambdaQueryWrapper<BaseRoleAppRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseRoleAppRelation::getRoleId, id);
        roleAppRelationService.remove(wrapper);
        return true;
    }

    @Override
    public Page<RoleVO> pageRoleVO(Page<RoleVO> page, Wrapper<BaseRole> wrapper) {
        return mapper.selectRoleVOPage(page, wrapper);
    }

}
